package com.chixing.commons;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    //加盐方式生成 md5
    public static String md5PassWord(String passowrd){
//      获取信息摘要器对象
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(passowrd.getBytes());
            StringBuffer buffer = new StringBuffer();
//            将byte做与运算
            for (byte b:result){
                //  与运算
                int number=b&0xff;//加盐
                String str = Integer.toHexString(number);
                if (str.length()==1){
                    buffer.append("0");
                }
                buffer.append(str);
            }
            //返回MD5加密后结果
            return buffer.toString();

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "MD5加密失败";
        }

    }

}
